import { specsList, specsCount } from '../../../util/request/api'
//封装菜单管理的状态管理
const state = {
    //菜单列表
    specslist: [],
    size: 3,
    page: 1,
    count: 0
}

const getters = {
    getSpecsList(state) {
        return state.specslist
    },
    getCount(state) {
        return state.count
    },
    //返回pageSize
    getSize(state) {
        return state.size
    }
}

const mutations = {
    REQ_SPECSLIST(state, payload) {
        state.specslist = payload
    },
    REQ_SPECSCOUNT(state, payload) {
        state.count = payload
    },
    //封装一个修改page的mutations
    REQ_CHANGEPAGE(state, payload) {
        state.page = payload
    }
}

const actions = {
    // 封装一个行动用于修改菜单列表
    getSpecsAction(context) {
        specsList({
            size: context.state.size,//查询条数
            page: context.state.page,//页码数
        })
            .then(res => {
                if (res.code == 200) {
                    context.commit('REQ_SPECSLIST', res.list)
                    if (context.state.page != 1 && res.list.length == 0) {
                        context.dispatch('changePageAction', context.state.page - 1)
                    }
                }
            })
    },
    //封装一个行动用于获取管理员总数
    getCountAction(context) {
        specsCount()
            .then(res => {
                console.log(res, '管理员总数');
                if (res.code == 200) {
                    context.commit('REQ_SPECSCOUNT', res.list[0].total)
                }
            })
    },
    //封装一个切换页面的行动
    changePageAction(context, payload) {
        context.commit('REQ_CHANGEPAGE', payload)
        //重新调用列表接口
        context.dispatch('getSpecsAction')
    }
}

export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}